﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CommandBase.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "UiCommonDialog.h"
#include "GbmpUiPlatformCommandIds.h"
#include "IDrawingLayoutManager.h"
#include "IDrawingLayout.h"
#include "IUserTransaction.h"
#include <QTreeWidgetItem>
#include "UiDocumentViewUtils.h"
#include "GcmpCommandState.h"
#include "IModelView.h"
#include "ElementsVisibleInLayoutFilter.h"
#include "IGenericElement.h"
#include "LayoutOrViewportParametersCustomizer.h"
#include "IElementParameters.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

//创建图纸
class CmdCreatePaper : public CommandBase
{
public:
    CmdCreatePaper();
    ~CmdCreatePaper();

public:
    // Command接口实现
    virtual gcmp::OwnerPtr<gcmp::IAction> ExecuteCommand(const gcmp::CommandParameters& cmdParams) override;
    virtual bool IsEnabled() const override;
    virtual bool IsVisible() const override;
};



CmdCreatePaper::CmdCreatePaper() :CommandBase(ID_CMD_CREATE_PAPER)
{
}

CmdCreatePaper::~CmdCreatePaper()
{
}

gcmp::OwnerPtr<IAction> CmdCreatePaper::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    std::wstring paperName = L"";
    UiCommonDialog::ButtonType buttonType = UiCommonDialog::ShowInputBox(GBMP_TR(L"创建图纸"), GBMP_TR(L"请输入要创建的图纸名称"), GBMP_TR(L"图纸1"), paperName);
    if (buttonType == UiCommonDialog::ButtonType::OK && paperName == L"")
    {
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"创建图纸失败"), GBMP_TR(L"图纸名称不能为空！"), (int)(UiCommonDialog::ButtonType::OK));
        return nullptr;
    }
    else if(buttonType != UiCommonDialog::ButtonType::OK)
        return nullptr;
    std::wstring errorMessage = L"";
    IDrawingLayoutManager* pLayoutsManager = IDrawingLayoutManager::Get(pDoc, &errorMessage);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pLayoutsManager, L"pLayoutsManager为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"创建图纸"), true);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opTransaction, L"opTransaction为空",L"GDMPLab",L"2024-03-30");
    errorMessage = L"";
    IDrawingLayout* pLayout = pLayoutsManager->CreateLayout(paperName, &errorMessage);
    if (!pLayout)
    {
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"创建图纸失败"), GBMP_TR(errorMessage), (int)(UiCommonDialog::ButtonType::OK));
        return nullptr;
    }
    IModelView* pModelView = pLayout->GetModelView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    pModelView->AddElementVisibilityFilter(NEW_AS_OWNER_PTR(ElementsVisibleInLayoutFilter));
    IGenericElement* pGenericElement = quick_cast<IGenericElement>(pLayout->GetOwnerElement());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGenericElement, L"pGenericElement为空",L"GDMPLab",L"2024-03-30");
    IElementParameters*  pElementParameters = pGenericElement->GetElementParameters();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElementParameters, L"pElementParameters为空",L"GDMPLab",L"2024-03-30");
    pElementParameters->SetElementParametersCustomizer(NEW_AS_OWNER_PTR(LayoutParametersCustomizer));
    opTransaction->Commit();

    return nullptr;
}

bool CmdCreatePaper::IsEnabled() const
{
    return GcmpCommandState::IsInProjectEnvironment() && !GcmpCommandState::IsInAnimationEnvironment();
}

bool CmdCreatePaper::IsVisible() const
{
    return GcmpCommandState::IsInProjectEnvironment() && !GcmpCommandState::IsInAnimationEnvironment();
}

REGISTER_GM_COMMAND(CmdCreatePaper)


